﻿Imports iConverter.Contracts
Imports System.Configuration


''' <summary>
''' 插件提供器
''' </summary>
''' <typeparam name="T">用于保存插件设置的强类型</typeparam>
''' <remarks></remarks>
Public MustInherit Class PluginProvider(Of T As ApplicationSettingsBase)
    Implements IPluginProvider




    ''' <summary>
    ''' 提供一个值用来指示插件是否需要配置
    ''' </summary>
    ''' <remarks></remarks>
    Public MustOverride ReadOnly Property CanConfigure As Boolean Implements Contracts.IPluginProvider.CanConfigure



    ''' <summary>
    ''' 获取用于插件设置界面的配置器
    ''' </summary>
    ''' <returns>
    ''' 一个实现的IPluginConfigurator接口的  System.Windows.Controls.UserControl 对象
    ''' 如果插件不需要设置，则返回Nothing
    ''' </returns>
    ''' <remarks></remarks>
    Public Function GetConfigurator() As Contracts.IPluginConfigurator Implements Contracts.IPluginProvider.GetConfigurator
        If Me.CanConfigure Then
            Return Configurator
        End If
        Return Nothing
    End Function

    ''' <summary>
    ''' 获取用于插件设置界面的配置器
    ''' </summary>
    Protected MustOverride ReadOnly Property Configurator() As IPluginConfigurator


    ''' <summary>
    ''' 获取用于保存插件设置对象的实例
    ''' </summary>
    ''' <returns>
    ''' 返回一个从ApplicationSettingsBase 派生的自定义强类型对象
    ''' 如果插件不需要设置，则返回Nothing
    ''' </returns>
    ''' <remarks></remarks>
    Private Function GetSettings() As System.Configuration.ApplicationSettingsBase Implements Contracts.IPluginProvider.GetSettings
        If Me.CanConfigure Then
            Return Settings
        End If
        Return Nothing
    End Function



    ''' <summary>
    ''' 获取用于保存插件设置对象的实例
    ''' </summary>
    Public MustOverride ReadOnly Property Settings As T

    Dim _plugins As List(Of Plugin(Of T))

    ''' <summary>
    ''' 获取提供器所有插件的实力
    ''' </summary>
    ''' <returns></returns>
    ''' <remarks></remarks>
    Public Function GetPlugins() As System.Collections.Generic.IEnumerable(Of Contracts.IPlugin) Implements Contracts.IPluginProvider.GetPlugins

        If _plugins Is Nothing Then
            _plugins = GeneratePlugins()

       
            For Each item In Me._plugins
                item.Provider = Me
            Next
        End If
        Return _plugins
    End Function

    ''' <summary>
    ''' 生成插件集合
    ''' </summary>
    ''' <returns></returns>
    ''' <remarks></remarks>
    Protected MustOverride Function GeneratePlugins() As List(Of Plugin(Of T))




End Class
